-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: introduce takeLast(n)
operator
#31
Conversation
* }}} | ||
*/ | ||
def takeLast(n: Int): List[T] = | ||
require(n > 0, "n must be > 0") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: Isn't 0
a legal case as well, where we would return an empty list? We'd probably need to drain
the source then to make the side effects "compatible" with calling takeLast
with n
> 0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Akka Streams requires n > 0. On the other hand, I don't see anything wrong with your idea @rucek.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, that was probably the inspiration. On the other hand, Scala collections use n.max(0)
- although I'm aware that collections might have different semantics than streams/channels. My own issue with allowing 0
for takeLast
is that it might require "artificially" draining the channel (so that it has a similar side effect on the channel to calling takeLast
with n > 0) - I'm not sure if it's a correct approach.
@adamw any thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nil + drain sounds most reasonable I guess :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the case when n == 0
to implementation and unit tests.
1ee90f4
to
3bb7325
Compare
Returns the list of up to `n` last elements from this source. Less than `n` elements is returned when this source contains les elements than requested. The [[List.empty]] is returned when `takeLast` is called on an empty source. Example: Source.empty[Int].takeLast(5) // List.empty Source.fromValues(1).takeLast(0) // List.empty Source.fromValues(1).takeLast(2) // List(1) val s = Source.fromValues(1, 2, 3, 4) s.takeLast(2) // List(4, 5) s.receive() // ChannelClosed.Done
3bb7325
to
fc367fe
Compare
Returns the list of up to
n
last elements from this source. Less thann
elements is returned when this source contains les elements than requested. The [[List.empty]] is returned whentakeLast
is called on an empty source.Example: